﻿package src.view 
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
  import org.papervision3d.materials.MovieMaterial;
  import src.event.proOperateEvent;
	
	/**
	 * 平面功能类
	 * @author	MeatHill
	 * @version	0.1(2009-06-04)
	 */
	public class flatFaceView extends Sprite
	{
		private static const _L_CONTEXT:LoaderContext = new LoaderContext(true);
		private static var _EVENT_HANDLERS:Object;
		
		private var _bg_loader:Loader;
		private var _width_uint:uint = 0;
		private var _height_uint:uint = 0;
    private var _ori_width_uint:uint = 0;
    private var _ori_height_uint:uint = 0;
    private var _material_container:MovieMaterial;
    private var _areas_arr:Array = [];
    private var _click_handler_obj:Object = { };
    private var _title_obj:Object = { };
    private var _content_obj:Object = { };
		
		public function flatFaceView(w:uint = 100, h:uint = 100, url:String = '') 
		{
			_width_uint = w;
			_height_uint = h;
      
      graphics.beginFill(0x000000);
      graphics.drawRect(0, 0, w, h);
      graphics.endFill();
			
			_bg_loader = new Loader();
			_bg_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, load_complete);
			_bg_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, load_failed);
			_bg_loader.load(new URLRequest(url));
		}
		
		/***************
		 * functions
		 * ************/
		private function load_complete(evt:Event):void {
			var _bmp:Bitmap = Bitmap(_bg_loader.content);
      _ori_width_uint = _bmp.width;
      _ori_height_uint = _bmp.height;
      _bmp.width = _width_uint;
      _bmp.height = _height_uint;
      _bmp.smoothing = true;
			addChild(_bmp);
			
      //trace("load " + _bg_urlr.url + " complete..");
			_bg_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, load_complete);
			_bg_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, load_failed);
			_bg_loader = null;
      
      //把热区也显示出来
      for (var i:uint = 0; i < _areas_arr.length; i++) {
        var _mc:Sprite = Sprite(_areas_arr[i]);
        _mc.x *= _width_uint / _ori_width_uint;
        _mc.y *= _height_uint / _ori_height_uint;
        _mc.width *= _width_uint / _ori_width_uint;
        _mc.height *= _height_uint / _ori_height_uint;
        addChild(_mc);
      }
      
      //通知材质重绘
      refresh_material();
		}
		private function load_failed(evt:IOErrorEvent):void {
      trace("load pic failed..");
			dispatchEvent(evt);
		}
		private function show_area(evt:MouseEvent):void {
      var _area_mc:Sprite = Sprite(evt.currentTarget);
			_area_mc.alpha = 1;
      refresh_material();
      
      // 显示浮动信息
      var _evt:proOperateEvent = new proOperateEvent(proOperateEvent.SHOW_ALT);
      _evt.title = _title_obj[_area_mc];
      _evt.content = _content_obj[_area_mc];
      dispatchEvent(_evt);
		}
		private function hide_area(evt:MouseEvent):void {
      var _area_mc:Sprite = Sprite(evt.currentTarget);
			_area_mc.alpha = 0;
      refresh_material();
      
      // 隐藏浮动信息
      var _evt:proOperateEvent = new proOperateEvent(proOperateEvent.REMOVE_ALT);
      dispatchEvent(_evt);
		}
    private function refresh_material():void {
      _material_container.drawBitmap();
    }
    private function click_handler(evt:MouseEvent):void {
      var _evt:proOperateEvent = new proOperateEvent(proOperateEvent.PRODUCT_OPERATE);
      _evt.op_type = _click_handler_obj[evt.currentTarget];
      dispatchEvent(_evt);
    }
    
    /***************
     * methods
     * ************/
    public function add_areas(xlist:XMLList):void {
			for each (var _area:XML in xlist) {
				var _area_mc:Sprite = new Sprite();
				_area_mc.x = uint(_area.@x);
				_area_mc.y = uint(_area.@y);
				_area_mc.graphics.lineStyle(0, 0x66ff00);
				_area_mc.graphics.beginFill(0x333333, .5);
				_area_mc.graphics.drawRect(0, 0, uint(_area.@w), uint(_area.@h));
				_area_mc.graphics.endFill();
        _area_mc.buttonMode = true;
        _area_mc.alpha = 0;
				_areas_arr.push(_area_mc);
				
				//绑定事件
				if (_area.@onmouseover) {
					_area_mc.addEventListener(MouseEvent.ROLL_OVER, this[_area.@onmouseover]);
				}
				if (_area.@onmouseout) {
					_area_mc.addEventListener(MouseEvent.ROLL_OUT, this[_area.@onmouseout]);
				}
        if (_area.@onclick) {
          _click_handler_obj[_area_mc] = _area.@onclick;
          _area_mc.addEventListener(MouseEvent.CLICK, click_handler);
        }
        _title_obj[_area_mc] = _area.@alt;
        _content_obj[_area_mc] = _area.toString();
			}
    }
    public function add_material_callback(src:MovieMaterial):void {
      _material_container = src;
    }
		
		/*******************
		 * static methods
		 * ****************/
	}
}